package cn.stylefeng.guns.sys.modular.tao.service.serviceImpl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.tao.entity.Classification;
import cn.stylefeng.guns.sys.modular.tao.entity.Goods;
import cn.stylefeng.guns.sys.modular.tao.mapper.GoodsMapper;
import cn.stylefeng.guns.sys.modular.tao.model.params.GoodsParam;
import cn.stylefeng.guns.sys.modular.tao.model.result.GoodsResult;
import cn.stylefeng.guns.sys.modular.tao.service.GoodsService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author 
 * @since 2019-08-09
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    private GoodsService goodsService;



    @Override
    public void add(GoodsParam param){
        Goods entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(GoodsParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(GoodsParam param){
        Goods oldEntity = getOldEntity(param);
        Goods newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public GoodsResult findBySpec(GoodsParam param){
        return null;
    }

    @Override
    public List<GoodsResult> findListBySpec(GoodsParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(GoodsParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<GoodsResult> getAllGoods(Map<String, Object> params) {
        String page = params.get("offset").toString();//页码
        String limit = params.get("limit").toString();//每页条数
        return goodsMapper.getAllGoods(params);
    }

    @Override
    public void updates(Goods goods) {
         goodsMapper.updates(goods);
    }

    @Override
    public void updateById(String id,String price) {
        goodsMapper.updateById(id,price);
    }

    @Override
    public Goods getByIds(Long id) {
        return goodsMapper.getByIds(id);
    }

    @Override
    public List<GoodsResult> getAllGoodsAndSku() {
        return goodsMapper.getAllGoodsAndSku();
    }

    @Override
    public List<Goods> getGoodsClass() {
        return goodsMapper.getGoodsClass();
    }

    @Override
    public Integer updateByClass(Classification classificationParam) {
        return goodsMapper.updateByClass(classificationParam);
    }

    @Override
    public List<String> getAllGoods2And() {
        return goodsMapper.getAllGoods2And();
    }

    @Override
    public List<GoodsResult> lists(Map<String, Object> map) {
        return goodsMapper.lists(map);
    }

    @Override
    public int counts(Map<String, Object> map) {
        return goodsMapper.counts(map);
    }

    @Override
    public  List<GoodsResult> getGoodsAll() {
        return goodsMapper.getGoodsAll();
    }

    @Override
    public GoodsResult getGoodsByGoodsId(String id) {
        return goodsMapper.getGoodsByGoodsId(id);
    }

    @Override
    public List<GoodsResult> getGoodsByName(String name,String index,String end) {
        return goodsMapper.getGoodsByName(name,index,end);
    }

    @Override
    public int getgoodsById(String goodId) {
        return goodsMapper.getgoodsById(goodId);
    }

    @Override
    public void addPrice(String goodId, String price) {
        goodsMapper.addPrice(goodId,price);
    }

    private Serializable getKey(GoodsParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Goods getOldEntity(GoodsParam param) {
        return this.getById(getKey(param));
    }

    private Goods getEntity(GoodsParam param) {
        Goods entity = new Goods();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }



    /**
     * 根据分类查询数据
     */
    @Override
    public List<GoodsResult> queryDataAccordingToClassification(@Param("ids") String ids){
        return goodsMapper.queryDataAccordingToClassification(ids);
    }

    @Override
    public T selectOne(Wrapper<T> queryWrapper) {
        return goodsService.selectOne(queryWrapper);
    }



    /**
     * 修改图文视频的id
     */
    public void updataPictureVideoId(Goods goods){
        goodsMapper.updataPictureVideoId(goods);
    }

    @Override
    public void updateType(GoodsParam goodsParam) {
        goodsMapper.updateType(goodsParam);
    }


}
